package com.happytoad.libs.riatrax
{
	import flash.display.DisplayObjectContainer;
	
	import mx.controls.Menu;
	import mx.core.Application;
	import mx.events.MenuEvent;
	
	/**
	 *  The Menu component includes automatic Google Analytic tracking.
	 * 
	 *  <br/><br/>
	 *  Copyright (c) 2009 HappyToad IT Consulting (<a href="http://www.happytoad.com" target="_blank">http://www.happytoad.com</a>)<br/>
     *  Created by Rich Tretola (<a href="http://blog.everythingflex.com" target="_blank">http://blog.everythingflex.com</a>)<br/>
	 *  
	 * @includeExample examples/MenuExample.mxml 
	 * 
	 * @see com.happytoad.libs.riatrax.Tracker
	 */
	  
	public class Menu extends mx.controls.Menu
	{
		include "SharedInclude.as";
		/**
     	 *  Constructor.
     	 */
		public function Menu()
		{
			super();
			this.addEventListener(MenuEvent.ITEM_CLICK,itemClicked);
		}
		/**
	     *  Creates and returns an instance of the Menu class. The Menu control's 
	     *  content is determined by the method's <code>mdp</code> argument. The 
	     *  Menu control is placed in the parent container specified by the 
	     *  method's <code>parent</code> argument.
	     * 
	     *  This method does not show the Menu control. Instead, 
	     *  this method just creates the Menu control and allows for modifications
	     *  to be made to the Menu instance before the Menu is shown. To show the 
	     *  Menu, call the <code>Menu.show()</code> method.
	     *
	     *  @param parent A container that the PopUpManager uses to place the Menu 
	     *  control in. The Menu control may not actually be parented by this object.
	     * 
	     *  @param mdp The data provider for the Menu control. 
	     *  @see mx.controls.Menu@dataProvider 
	     * 
	     *  @param showRoot A Boolean flag that specifies whether to display the 
	     *  root node of the data provider.
	     *  @see mx.controls.Menu@showRoot 
	     * 
	     *  @return An instance of the Menu class. 
	     *
	     *  @see mx.controls.Menu#popUpMenu()
	     */
		public static function createMenu(parent:DisplayObjectContainer, mdp:Object, showRoot:Boolean = true):com.happytoad.libs.riatrax.Menu
    	{
	        var menu:com.happytoad.libs.riatrax.Menu = new com.happytoad.libs.riatrax.Menu();
	        menu.tabEnabled = false;
	        menu.owner = DisplayObjectContainer(Application.application);
	        menu.showRoot = showRoot;
	        popUpMenu(menu, parent, mdp);
	        return menu;
    	}
		/**
	     *  @private
	     *  EventListener for MenuEvent.ITEM_CLICK
	     *  calls trackIt
	     *  if no gaCategory is supplied, className will be recorded
	     *  if no gaAction is supplied, event.type will be recorded
	     *  if no gaLabel is supplied, event.item[labelField] will be recorded
	     */
		private function itemClicked(event:MenuEvent):void{
			var o:com.happytoad.libs.riatrax.Menu = event.target as com.happytoad.libs.riatrax.Menu;
			trackIt(gaCategory != null ? gaCategory : o.className,
				    gaAction != null ? gaAction : event.type,
				    gaLabel != null ? gaLabel : event.item[labelField],
				    gaValue);
		}
	}
}